﻿using System;
using System.Threading.Tasks;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Async;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArcGIS.Client.Async.Tests
{
    [TestClass]
    public class LayerTests : SilverlightTest
    {
        private ArcGISDynamicMapServiceLayer _layer;

        [TestInitialize]
        public void TestInitialize()
        {
            _layer = new ArcGISDynamicMapServiceLayer
            {
                Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"
            };
        }

        [TestMethod, Asynchronous]
        public async void Initialize()
        {
            await _layer.InitializeTaskAsync();
            TestComplete();
        }

        [TestMethod, Asynchronous, Description("Should be OK")]
        public async void MultipleInitializations()
        {
            var task1 = _layer.InitializeTaskAsync();
            var task2 = _layer.InitializeTaskAsync();
            await TaskEx.WhenAll(task1, task2);
            await _layer.InitializeTaskAsync();
            TestComplete();
        }

        [TestMethod, Asynchronous]
        public async void InitializationFailure()
        {
            var badLayer = new ArcGISDynamicMapServiceLayer
            {
                Url = "http://WRONGSERVER.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"
            };
            try
            {
                await badLayer.InitializeTaskAsync();
            }
            catch (Exception)
            {
                TestComplete();
                return;
            }
            Assert.Fail("Should not be here");
        }

        [TestMethod, Asynchronous]
        public async void MultipleInitializationFailures()
        {
            bool exceptionRaised = false;
            var badLayer = new ArcGISDynamicMapServiceLayer
            {
                Url = "http://WRONGSERVER.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"
            };

            try
            {
                await badLayer.InitializeTaskAsync();
            }
            catch (Exception)
            {
                exceptionRaised = true;
            }
            if (exceptionRaised)
            {
                try
                {
                    // second try
                    await badLayer.InitializeTaskAsync();
                }
                catch (Exception)
                {
                    TestComplete();
                    return;
                }
            }
            Assert.Fail("Should not be here");
        }

        [TestMethod, Asynchronous]
        public async void ReinitializeAfterFailure()
        {
            bool exceptionRaised = false;
            var badLayer = new ArcGISDynamicMapServiceLayer
            {
                Url = "http://WRONGSERVER.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"
            };

            try
            {
                await badLayer.InitializeTaskAsync();
            }
            catch (Exception)
            {
                exceptionRaised = true;
                // setting good URL. Should reset IsInitialized.
                Assert.IsTrue(badLayer.IsInitialized);
                badLayer.Url = _layer.Url;
                Assert.IsFalse(badLayer.IsInitialized);
            }
            if (exceptionRaised)
            {
                try
                {
                    // second try
                    await badLayer.InitializeTaskAsync();
                }
                catch (Exception)
                {
                    Assert.Fail("Should not be here");
                }
                TestComplete();
                return;
            }
            Assert.Fail("Should not be here");            
        }
    }
}
